/*
 * @Author: szx
 * @Date: 2022-03-13 15:44:28
 * @LastEditTime: 2022-03-13 15:54:25
 * @Description:
 * @FilePath: \learn\MDN_JS学习\八股文\防抖和节流\throttle.js
 */
var count = 1;
var container = document.getElementById('container');

function getUserAction() {
    container.innerHTML = count++;
}

// function throttle(fn, wait) {
//     let activeTime = 0;
//     return function () {
//         console.log('?');
//         const now = +new Date();
//         // 若已等待了 wait 毫秒，则能重新触发
//         if (now - activeTime >= wait) {
//             fn.apply(this, arguments);
//             activeTime = now;
//         }
//     };
// }

// 第二版
function throttle(fn, wait) {
    let timeout;
    return function () {
        if (!timeout) {
            timeout = setTimeout(function () {
                timeout = null;
                fn.apply(this, arguments);
            }, wait);
        }
    };
}


function throttle(fn, wait) {
    let timeout;
    return function () {
        if (!timeout) {
            fn.apply(this, arguments);
            timeout = setTimeout(function () {
                timeout = null;
            }, wait);
        }
    };
}
container.onmousemove = throttle(getUserAction, 1000);
